#include "smelter_blink.h"
#include "helper.h"
#include "debug.h"

typedef enum
{
    SBS_OFF,
    SBS_ON,
    SBS_CNT,
} Smelter_Blink_Stage;
typedef struct
{
    su8 curCount;
    su8 onCount;
    su8 offCount;
    su8 curStage;
} Smelter_Blink_Context;
static Smelter_Blink_Context context;

void smelter_blink_restart(Config_Mode_Blink *blink)
{
    context.curCount = 0;
    context.onCount = 0;
    context.offCount = 0;
    context.curStage = SBS_CNT;
}

Efct_Stage smelter_blink_process(Config_Mode_Blink *blink)
{
    su16 tmp;
    Efct_Stage ret = E_PROCESS;
    switch (context.curStage)
    {
    case SBS_OFF:
        for (tmp = 0; tmp < gCfgFactoryPtr->enabledDotNumber; tmp++)
        {
            rgbStrip.colors[tmp].R = 0;
            rgbStrip.colors[tmp].G = 0;
            rgbStrip.colors[tmp].B = 0;
        }
        context.offCount++;
        if (context.offCount >= blink->blinkOffTimes)
        {
            context.offCount = 0;
            context.curStage = SBS_ON;
        }
        break;
    case SBS_ON:
        for (tmp = 0; tmp < gCfgFactoryPtr->enabledDotNumber; tmp++)
        {
            rgbStrip.colors[tmp].R = blink->color.R;
            rgbStrip.colors[tmp].G = blink->color.G;
            rgbStrip.colors[tmp].B = blink->color.B;
        }

        context.onCount++;

        if (context.onCount >= blink->blinkOnTimes)
        {
            context.onCount = 0;
            context.curStage = SBS_CNT;
        }

        break;
    case SBS_CNT:
        for (tmp = 0; tmp < gCfgFactoryPtr->enabledDotNumber; tmp++)
        {
            rgbStrip.colors[tmp].R = 0;
            rgbStrip.colors[tmp].G = 0;
            rgbStrip.colors[tmp].B = 0;
        }

        if (context.curCount >= blink->blinkTimes)
        {
            ret = E_FINISHED;
        }
        else
        {
            context.curCount++;
            ret = E_PROCESS;
            context.curStage = SBS_OFF;
        }
        break;
    default:
        break;
    }
    return ret;
}